<?php

/**
 * This is the model class for table "{{slideshow}}".
 *
 * The followings are the available columns in table '{{slideshow}}':
 * @property integer $id
 * @property integer $image
 * @property string $description_vi
 * @property string $description_en
 * @property integer $createddate
 * @property integer $modifieddate
 * @property integer $enabled
 * @property integer $sort
 *
 * The followings are the available model relations:
 * @property File $file
 */
class Slideshow extends CActiveRecord
{
  public $thumbnails = array(
    array('100','50'),
    array('1060','300')
  );
  
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Slideshow the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{slideshow}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('image', 'required', 'on' => 'create'),
			array('image', 'file', 'types' => 'jpg, jpeg, gif, png', 'allowEmpty' => true),
			//array('image, createddate, modifieddate, enabled, sort', 'numerical', 'integerOnly'=>true),
			array('description_vi, description_en', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, image, description_vi, description_en, createddate, modifieddate, enabled, sort', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'file' => array(self::BELONGS_TO, 'File', 'image'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'image' => Yii::t('default', 'Image'),
			'description_vi' => Yii::t('default', 'Description') . ' (VI)',
			'description_en' => Yii::t('default', 'Description') . ' (EN)',
			'createddate' => 'Createddate',
			'modifieddate' => 'Modifieddate',
			'enabled' => 'Enabled',
			'sort' => 'Sort',
		);
	}

  protected function beforeSave() {
    if($this->isNewRecord) {
      $this->createddate = time();
    }
    $this->modifieddate = time();
    return parent::beforeSave();
  }
	
  public function getSlideshowFile($slideshowID, $w, $h)
  {
    $slideshow = Slideshow::model()->findByPk($slideshowID);
    
    $image = 'no_image.jpg';
    if (!empty($slideshow) && $slideshow->image != '') {
      $file = File::model()->findByPk($slideshow->image);
      if (!empty($file) && file_exists(Yii::app()->basePath . "/../upload/slideshow/thumbs_$w"."x"."$h/" . $file->filesaved)) {
        $image = $file->filesaved;
      }
    }
    return $image;
  }
  
  public function getSlideshowImage($slideshowID, $w, $h)
  {
    $linkfile = $this->getSlideshowFile($slideshowID, $w, $h);
    $image    = CHtml::image(Yii::app()->baseUrl . "/upload/slideshow/thumbs_$w" . "x" . "$h/$linkfile");
    return $image;
  }
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
    $criteria->order = 'sort ASC';
    
		$criteria->compare('id',$this->id);
		$criteria->compare('image',$this->image);
		$criteria->compare('description_vi',$this->description_vi,true);
		$criteria->compare('description_en',$this->description_en,true);
		$criteria->compare('createddate',$this->createddate);
		$criteria->compare('modifieddate',$this->modifieddate);
		$criteria->compare('enabled',$this->enabled);
		$criteria->compare('sort',$this->sort);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		  'sort'=>array(
        'attributes'=>array(
          'name'=>array(  
            'asc'=>'l.name',
            'desc'=>'l.name DESC',
          ),
          '*',
        ),
      ),
      'pagination' => array(
        'pageSize' => Yii::app()->user->getState('pageSize', 10)
      )
		));
	}
}